/*---------------------------------------------------------------------
 * File name: registers.cpp
 *
 * Copyright (c) <2020-2022>, <ChenLong>
 *
 * All rights reserved.
 *
 * Author: ChenLong
 * Email: worldlong@foxmail.com
 *--------------------------------------------------------------------*/
#include "registers.h"
/*
*/
__no_init uint16_t reg_table[REG_NUMBER];
/*
*/
static uint16_t nvd_reg_table[][3]={
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)120, (uint16_t)-1600, (uint16_t)1600},
{(uint16_t)30, (uint16_t)10, (uint16_t)60},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
{(uint16_t)0, (uint16_t)0, (uint16_t)0},
};
/*
*/
void nvd_reg_reset()
{
    for(int i = NVD_REG_START; i <= NVD_REG_END; i++) {
        reg_table[i] = nvd_reg_table[i - NVD_REG_START][0];
    }
}
/*
*/
void nvd_reg_limit(int addr, uint16_t data)
{
    int index = addr - NVD_REG_START;
    if(data < nvd_reg_table[index][1]) {
        data = nvd_reg_table[index][1];
    } else if(data > nvd_reg_table[index][2]) {
        data = nvd_reg_table[index][2];
    }
}
/*
*/
void nvd_reg_limit(int addr, int16_t data)
{
    int index = addr - NVD_REG_START;
    if(data < (int16_t)nvd_reg_table[index][1]) {
        data = (int16_t)nvd_reg_table[index][1];
    } else if(data > (int16_t)nvd_reg_table[index][2]) {
        data = (int16_t)nvd_reg_table[index][2];
    }
}
/*
*/
void reg16_write(int addr, uint16_t data)
{
    reg_table[addr] = data;
}
/*
*/
void reg32_write(int addr, uint32_t data)
{
    reg_table[addr] = data;
    reg_table[addr + 1] = data >> 16;
}
/*
*/
void reg48_write(int addr, uint64_t data)
{
    reg_table[addr] = data;
    reg_table[addr + 1] = data >> 16;
    reg_table[addr + 2] = data >> 32;
}
/*
*/
void reg64_write(int addr, uint64_t data)
{
    reg_table[addr] = data;
    reg_table[addr + 1] = data >> 16;
    reg_table[addr + 2] = data >> 32;
    reg_table[addr + 3] = data >> 48;
}
/*
*/
uint16_t reg16_read(int addr)
{
    return reg_table[addr];
}
/*
*/
uint32_t reg32_read(int addr)
{
    return ((uint32_t)reg_table[addr + 1] << 16) + (uint32_t)reg_table[addr];
}
/*
*/
uint64_t reg48_read(int addr)
{
    return ((uint64_t)reg_table[addr + 2] << 32) +
           ((uint64_t)reg_table[addr + 1] << 16) +
            (uint64_t)reg_table[addr];
}
/*
*/
uint64_t reg64_read(int addr)
{
    return ((uint64_t)reg_table[addr + 3] << 48) +
           ((uint64_t)reg_table[addr + 2] << 32) +
           ((uint64_t)reg_table[addr + 1] << 16) +
            (uint64_t)reg_table[addr];
}
